import axios from "axios";

const baseURL = 'https://wwts.yzjtech.com/prod_api'

class HttpRequest {
    constructor(baseURL) {
        this.baseURL = baseURL
    }

    getInsideConfig() {
        const config = {
            baseURL: this.baseURL,
            header: {}
        }
        return config
    }
    interception(instanca, isGetData) {
        instanca.interceptors.request.use(function (config) {
            //  注入token
            const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ5aWQiOiJ1c2VyX2Q0MjlmNzIwLWVhODMtNDk3NS1hOWZjLTBkOTY2Y2VlMzk4MiIsImlhdCI6MTcxNjgwMzk3NywiZXhwIjoxNzE3NDA4Nzc3fQ._5_PxeC52JH9QQK0rjpeBpqKDV5TDJcJYslKZT2ipnY'
            if (token) config.headers.Authorization = `Bearer ${token}`
            config.method = config.method.toUpperCase()
            return config;
        }, function (error) {
            return Promise.reject(error);
        });

        // 添加响应拦截器
        instanca.interceptors.response.use(function (response) {
            // todo 根据实际情况修改  还需根据不同状态码 进行响应操作
            return isGetData ? response.data.data: response;
        }, function (error) {
            return Promise.reject(error);
        });
        return instanca
    }

    requset(options, {isGetData = true}) {
        options = {...this.getInsideConfig(), ...options}
        // 创建axios 实例
        const instanca = axios.create()
        // 挂载拦截器
        this.interception(instanca, isGetData)
        return instanca(options)
    }


}

export default new HttpRequest(baseURL)